package ui;


import javax.swing.*;
import javax.swing.border.EmptyBorder;

import core.PizzaStoreFacadeInterface;

import ui.handlers.MenuHandler;
import ui.handlers.RoleHandler;

import java.awt.*;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;


public class HomeScreen extends BaseScreen {

	private JButton cashier_btn;
	private JButton chef_btn;
	private JButton manager_btn;
	private JButton viewMenu_btn;
	
	private JFrame mainFrame;
	
	/* This methods are used for getting the button objects so they can be compared in the handler */
	
	public JButton getCashierButton()
	{ return cashier_btn; }
	
	public JButton getChefButton()
	{ return chef_btn; }
	
	public JButton getManagerButton()
	{ return manager_btn; }
	
	public JButton getViewMenuButton()
	{ return viewMenu_btn; }
	
	/* END OF BUTTON METHODS */
	
	public HomeScreen(PizzaStoreFacadeInterface facade)
	{
		super(facade);
		
		mainFrame = new JFrame();
		mainFrame.setTitle("Home Screen");
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setSize(650, 550);
		mainFrame.setLayout(new GridLayout(4, 1));
		
		doTitle();
		JPanel empty = new JPanel();
		mainFrame.add(empty);
		doButtons();
		
		mainFrame.setVisible(true);
	}
	
	private void doTitle()
	{
		String title = "<HTML><PRE><FONT SIZE = \"7\">" +
			"Welcome to the Pizza Store<br/>\t\tManagement System</FONT><PRE></HTML>";
		JLabel title_lbl = new JLabel(title, JLabel.CENTER);
		title_lbl.setBorder(new EmptyBorder(70, 0, 0, 0));
		
		mainFrame.add(title_lbl);
	}
	
	private void doButtons()
	{
		String text = "<HTML><FONT SIZE = \"6\">" +
			"Please select your role:<br/></FONT></HTML>";
		JLabel text_lbl = new JLabel(text);
		text_lbl.setBorder(new EmptyBorder(0, 50, 0, 0));
		
		cashier_btn = new JButton("Cashier");
		cashier_btn.setBorder(new EmptyBorder(10, 40, 10, 40));
		Font btnFont = new Font(cashier_btn.getFont().getName(),
				cashier_btn.getFont().getStyle(), 16);
		cashier_btn.setFont(btnFont);
		cashier_btn.addActionListener(new RoleHandler(this));
		
		chef_btn = new JButton("Chef");
		chef_btn.setBorder(new EmptyBorder(10, 40, 10, 40));
		chef_btn.setFont(btnFont);
		chef_btn.addActionListener(new RoleHandler(this));
		
		manager_btn = new JButton("Manager");
		manager_btn.setBorder(new EmptyBorder(10, 40, 10, 40));
		manager_btn.setFont(btnFont);
		manager_btn.addActionListener(new RoleHandler(this));
		
		viewMenu_btn = new JButton("ViewMenu");
		viewMenu_btn.setFont(btnFont);
		viewMenu_btn.addActionListener(new MenuHandler(this));
		
		JPanel cashier_panel = new JPanel();
		cashier_panel.setBorder(new EmptyBorder(0, 0, 0, 30));
		cashier_panel.add(cashier_btn);
		
		JPanel chef_panel = new JPanel();
		chef_panel.setBorder(new EmptyBorder(0, 30, 0, 0));
		chef_panel.add(chef_btn);
		
		JPanel manager_panel = new JPanel();
		manager_panel.setBorder(new EmptyBorder(0, 30, 0, 30));
		manager_panel.add(manager_btn);
		
		JPanel viewMenu_panel = new JPanel();
		viewMenu_panel.setBorder(new EmptyBorder(10, 0, 0, 0));
		viewMenu_panel.add(viewMenu_btn);
		
		JPanel button_panel = new JPanel();
		button_panel.setLayout(new FlowLayout());
		button_panel.add(cashier_panel);
		button_panel.add(manager_panel);
		button_panel.add(chef_panel);
		button_panel.add(viewMenu_panel);
		
		mainFrame.add(text_lbl);
		mainFrame.add(button_panel);
	}
	
	public static void main(String[] args) throws RemoteException
	{
		String hostname, port;
		
		if(args.length >= 2){
			port = args[1];
		}else{
			port = "1099";//default port
		}
		if(args.length >= 1){
			hostname = args[0];
		}else{
			hostname = "localhost";//default host
		}
		
		String url = "rmi://"+hostname+":"+port;
		
		PizzaStoreFacadeInterface facade = null;
		try {
			facade = (PizzaStoreFacadeInterface) Naming.lookup(url+"/"+"PizzaStoreFacade");
			System.out.println("Bound to PizzaStoreFacade");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		new HomeScreen(facade);		
	}
}